package com.wanjl.spring_05_mybatis_spring;

import com.wanjl.spring_05_mybatis_spring.common.utils.SqlSessionFactoryUtil;
import com.wanjl.spring_05_mybatis_spring.entity.Tea;
import com.wanjl.spring_05_mybatis_spring.mapper.TeaMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author WanJl
 * @version 1.0
 * @title Main
 * @description
 * @create 2024/11/1
 */
public class Main {
    public static void main(String[] args) throws IOException {
        //加载核心配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis-config.xml");
        //使用SqlSessionFactoryBuilder对象的build方法，以resource为参数，创建SQL会话工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        //获取session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取Mapper的对象
        TeaMapper teaMapper = sqlSession.getMapper(TeaMapper.class);
        //执行查询
        List<Tea> teaList = teaMapper.selectAll();
        //输出结果
        teaList.forEach(System.out::println);
    }
}
